// MyGraphButtonView.cpp : implementation file
//

#include "stdafx.h"
#include "waveeditor.h"
#include "waveeditorDoc.h"
#include "MyGraphFrame.h"	
#include "MyGraphButtonView.h"
#include "MyGraphView.h"
#include <list>
#include <algorithm>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// MyGraphButtonView

IMPLEMENT_DYNCREATE(GraphButtonView, CScrollView)

GraphButtonView::GraphButtonView()
: CaptureType(UNCAPTURED)
, captured_signal_number(0)
, oldpoint(0)
, wmaxx(0)
{
}

GraphButtonView::~GraphButtonView()
{
}


BEGIN_MESSAGE_MAP(GraphButtonView, CScrollView)
	//{{AFX_MSG_MAP(MyGraphView)
	ON_WM_KILLFOCUS()
	//}}AFX_MSG_MAP
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_WM_VSCROLL()
	ON_WM_MOUSEWHEEL()
	ON_WM_CONTEXTMENU()
	ON_WM_KEYDOWN()
	ON_COMMAND(ID_SET_HIGHLIGHTING_RULES_MENU,			OnSetHighlightingRules)
	ON_COMMAND(ID_GROUP_WAVES_MENU,						OnGroupWaves)
	ON_COMMAND(ID_UN_GROUP_WAVES,						OnUnGroup)
	ON_COMMAND(ID_COLOR_SIGNAL,							OnChooseColorSignal)
	ON_COMMAND(ID_REMOVE_SIGNAL,						OnRemoveSignal)
	ON_COMMAND(ID_DELETE_SIGNAL,						OnDeleteSignal)
	ON_COMMAND(ID_SORT_WAVES_BY_NAME,					OnSortSelectedWavesByName)
	ON_COMMAND(ID_SORT_WAVES_BY_SHIFT_GREEN_MARK,		OnSortSelectedWavesByVal)
	ON_COMMAND(ID_DESELECT_ALL_WAVES,					OnDeselectAllWaves)
	ON_COMMAND(ID_DISP_TO_FIRST_CHANGE_AND_MAX_ZOOMIN,	OnDisplaceToFirstChangeAndMaxZoomin)

	ON_UPDATE_COMMAND_UI(ID_SET_HIGHLIGHTING_RULES_MENU,		OnUpdateSetHighlightingRulesButton)
	ON_UPDATE_COMMAND_UI(ID_GROUP_WAVES_MENU,					OnUpdateGroupWavesButton)
	ON_UPDATE_COMMAND_UI(ID_UN_GROUP_WAVES,						OnUpdateUnGroupButton)
	ON_UPDATE_COMMAND_UI(ID_COLOR_SIGNAL,						OnUpdateChooseColorSignalButton)
	ON_UPDATE_COMMAND_UI(ID_REMOVE_SIGNAL,						OnUpdateRemoveSignalButton)
	ON_UPDATE_COMMAND_UI(ID_DELETE_SIGNAL,						OnUpdateDeleteSignalButton)
	ON_UPDATE_COMMAND_UI(ID_SORT_WAVES_BY_NAME,					OnUpdateSortWaveByNameButton)
	ON_UPDATE_COMMAND_UI(ID_SORT_WAVES_BY_SHIFT_GREEN_MARK,		OnUpdateSortWaveByValButton)
	ON_UPDATE_COMMAND_UI(ID_DESELECT_ALL_WAVES,					OnUpdateDeselectAllWavesButton)
	ON_UPDATE_COMMAND_UI(ID_DISP_TO_FIRST_CHANGE_AND_MAX_ZOOMIN,OnUpdateDisplaceToFirstChangeAndMaxZoominButton)

	ON_WM_INITMENUPOPUP()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// MyGraphView drawing

void GraphButtonView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	SelectedScenario = pDoc->selectedscenario;
	sizeTotal.cx =100;
	sizeTotal.cy = pDoc->_script[SelectedScenario]->globalsignals->GetTotalSignalDelta();
	SetScrollSizes(MM_TEXT, sizeTotal);
	int	bnum	=	0;
	wmaxx		=	100;
	for(unsigned int pos= 0;pos<pDoc->_script[SelectedScenario]->globalsignals->GetNumberOfSignals();pos++)
	{
		if(pDoc->_script[SelectedScenario]->globalsignals->IfShowSignal(pos)== TRUE)
		{
			CString	btitle	=	pDoc->_script[SelectedScenario]->globalsignals->GetSignalName(pos);
			if(pDoc->_script[SelectedScenario]->globalsignals->CheckIfSignalWasUsed(pos)	==	FALSE)
			{
				btitle	+=	" not used";
			}

//			MyButton[bnum].Create(	btitle,WS_CHILD|WS_VISIBLE, 
//									CRect(	0,pDoc->_globalsignals.GetSignalPos(pos),
//											wmaxx,pDoc->_globalsignals.GetSignalEndPos(pos)),
//									this, 4000+bnum);
//			pDoc->_globalsignals.AssignSignalToButton(pos,4000+bnum);
			bnum++;
		}
	}
}
	
void GraphButtonView::OnDraw(CDC* pDC)
{
	// TODO: add draw code here
	ReadGraphData(pDC);
}

/////////////////////////////////////////////////////////////////////////////
// MyGraphView diagnostics

#ifdef _DEBUG
void GraphButtonView::AssertValid() const
{
	CScrollView::AssertValid();
}

void GraphButtonView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

ZWaveeditorDoc* GraphButtonView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ZWaveeditorDoc)));
	return (ZWaveeditorDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// MyGraphView message handlers
void	GraphButtonView::ChangeWindowTitle()
{
	CFrameWnd* pFrame	=	GetParentFrame();
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CString	name	=	"Graph View (";
	name			+=	pDoc->GetTitle()+")" + " - "+pDoc->GroupName;
	pFrame->SetWindowText(name);
	if(pDoc->CheckIfGraphWindowOpened() == FALSE)
	{
		Invalidate();
	}


}

void GraphButtonView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	ChangeWindowTitle();
}

void GraphButtonView::UpdateView()
{
	ChangeWindowTitle();
	Invalidate();
}

void GraphButtonView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	// TODO: Add your specialized code here and/or call the base class
	ChangeWindowTitle();	
	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}

void GraphButtonView::OnKillFocus(CWnd* pNewWnd) 
{
	CScrollView::OnKillFocus(pNewWnd);
	ChangeWindowTitle()	;
	// TODO: Add your message handler code here
	
}

int GraphButtonView::ReadGraphData(CDC* pDC)
{
	ScenarioSignals* Globalsignals = GetDocument()->_script[SelectedScenario]->globalsignals;
	CRect winrect;
	GetClientRect(&winrect);
	wmaxx	=	winrect.right;
	long StartDrawingCoordinateY = this->GetScrollPos( SB_VERT );
	long EndDrawingCoordinateY = StartDrawingCoordinateY + winrect.bottom;
	long StartSignalPosition;
	long EndSignalPosition;
	pDC->SetViewportOrg(-GetScrollPos(SB_HORZ   ),-GetScrollPos(SB_VERT   ));
	for(unsigned int pos= 0;pos<Globalsignals->GetNumberOfSignals();pos++)
	{
		StartSignalPosition = Globalsignals->GetSignalPos(pos);
		EndSignalPosition = Globalsignals->GetSignalEndPos(pos);
		if(Globalsignals->IfShowSignal(pos)== TRUE &&
		  StartDrawingCoordinateY < EndSignalPosition &&
		  StartSignalPosition < EndDrawingCoordinateY )
		{
			DrawFullButton( pDC, pos, wmaxx);
		}
	}
	return 0;
}


void GraphButtonView::DrawButtonBar(CPoint upperleft)
{
	if ( upperleft.y < 0 )
	{
		return;
	}
	CRect ButtonBarRect( upperleft, CPoint(wmaxx, GetDocument()->_script[SelectedScenario]->globalsignals->GetSignalEndPos( GetDocument()->_script[SelectedScenario]->globalsignals->GetSignalNumber( upperleft )) ));
	DrawBar( ButtonBarRect );
}

void GraphButtonView::DrawInGroupRectBar( int SignalNumber, int SignalInGroupNumber )
{
	CPoint SignalInGroupPositions = GetDocument()->_script[SelectedScenario]->globalsignals->GetPositionsOfSignalInGroup( SignalNumber, SignalInGroupNumber );
	CRect SignalInGroupBarRect( 0, SignalInGroupPositions.x, wmaxx, SignalInGroupPositions.y );
	DrawBar( SignalInGroupBarRect );						
}

void GraphButtonView::DrawBar( CRect BarRect )
{
	ChildGraphFrame	*pFrame	=	(ChildGraphFrame*) GetParentFrame();
	CDC		*pDC			=	pFrame->GetGraphBut()->GetDC();
	pDC->SetViewportOrg(-GetScrollPos(SB_HORZ   ),-GetScrollPos(SB_VERT   ));
	CPen pen(PS_SOLID,3,RGB(128,128,128));
	CPen *oldpen	=	pDC->SelectObject(&pen);
	int oldmode		=	pDC->SetROP2(R2_XORPEN );
	CPoint	p[5];
	p[0]	=	p[1]	=	p[2]	=	p[3]	=	p[4]	= BarRect.TopLeft();
	p[1].x	=	p[2].x	=	BarRect.right;
	p[2].y	=	p[3].y	=	BarRect.bottom;
	pDC->Polyline(p,5);
	pDC->SetROP2(oldmode);
	pDC->SelectObject(oldpen);
	pFrame->GetGraphBut()->ReleaseDC( pDC );
}

void GraphButtonView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	CScrollView::OnLButtonDown(nFlags, point);
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CPoint p	=	point;
	p.x			=	0;
	p.y			+=	GetScrollPos(SB_VERT   );
	int signum	=	pDoc->_script[SelectedScenario]->globalsignals->GetSignalNumber(p);
	if( signum >= 0 )
	{
		SetCapture();
		captured_signal_number	=	signum;
		if ( (nFlags & MK_CONTROL) != 0 )
		{
			CaptureType = SIGNAL_INSIDE_GROUP_CAPTURED;
			SignalPositionNumberInGroup = pDoc->_script[SelectedScenario]->globalsignals->GetSignalPositionNumberInGroupFromButtonView( p.y );
			DrawInGroupRectBar( captured_signal_number, SignalPositionNumberInGroup );
		}
		else if ( nFlags & MK_SHIFT )
		{
			pDoc->_script[SelectedScenario]->globalsignals->MarkAllSignalsBetween(p);
			Invalidate();
		}
		else
		{
			p.y						=	pDoc->_script[SelectedScenario]->globalsignals->GetButtonY(p);
			CaptureType				=	SIGNAL_CAPTURED;
			oldpoint				=	p;
			DrawButtonBar(oldpoint);
		}
	}
}

void GraphButtonView::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	//CScrollView::OnLButtonDown(nFlags, point);
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	long VerticalScrollPos = GetScrollPos( SB_VERT );
	CPoint p	=	point;
	p.x			=	0;
	p.y			+= VerticalScrollPos ;
	CScrollView::OnMouseMove(nFlags, point);
	switch (CaptureType)
	{
	case SIGNAL_CAPTURED:
		{
			p.y			=	pDoc->_script[SelectedScenario]->globalsignals->GetButtonY(p);
			if ( p.y >= 0 )
			{
				DrawButtonBar(oldpoint);
				oldpoint=p;
				DrawButtonBar(oldpoint);
			}
			break;
		}
	case SIGNAL_INSIDE_GROUP_CAPTURED:
		{
			int NewSignalPositionNumberInGroup = pDoc->_script[SelectedScenario]->globalsignals->MoveSignalsInGroup( captured_signal_number, SignalPositionNumberInGroup, p );
			if ( NewSignalPositionNumberInGroup != SignalPositionNumberInGroup )
			{
				CRect WindowRect;
				this->GetWindowRect( &WindowRect );
				ChildGraphFrame	*pFrame	=	(ChildGraphFrame*) GetParentFrame();
				pFrame->GetGraphWnd()->InvalidateRect( CRect(0, 
															 pDoc->_script[SelectedScenario]->globalsignals->GetSignalPos(captured_signal_number) - VerticalScrollPos,
															 pDoc->graphwindowMaxX,
															 pDoc->_script[SelectedScenario]->globalsignals->GetSignalEndPos(captured_signal_number) - VerticalScrollPos ));
				SignalPositionNumberInGroup = NewSignalPositionNumberInGroup;
				CDC * pDC = GetDC();
				pDC->SetViewportOrg(-GetScrollPos(SB_HORZ   ),-GetScrollPos(SB_VERT   ));
				DrawFullButton( pDC, captured_signal_number, WindowRect.right - WindowRect.left );
				ReleaseDC( pDC );
				DrawInGroupRectBar( captured_signal_number, SignalPositionNumberInGroup );
			}
			break;
		}
	}
}

void GraphButtonView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	CScrollView::OnLButtonUp(nFlags, point);
	if( CaptureType	== SIGNAL_CAPTURED)
	{
		DrawButtonBar(oldpoint);
		ZWaveeditorDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);
		CPoint p	=	point;
		p.x			=	0;
		p.y			+=	GetScrollPos(SB_VERT);
		if ( p.y < 0 )
		{
			p.y = 0;
		}

		int num;
		ChildGraphFrame	*pFrame	= (ChildGraphFrame*) GetParentFrame();

		int CupPos = pDoc->_script[SelectedScenario]->globalsignals->GetSignalPos(captured_signal_number);
		int CupEndPos = pDoc->_script[SelectedScenario]->globalsignals->GetSignalEndPos(captured_signal_number);

		if (pDoc->_script[SelectedScenario]->globalsignals->MoreThanOneMarked()
			&& ( ( p.y < CupPos ) || ( p.y > CupEndPos ) )
			&& ( pDoc->_script[SelectedScenario]->globalsignals->CheckIfSignalMarked( captured_signal_number ) ) )
		{
			long StartDrawingCoordinateY = this->GetScrollPos( SB_VERT );
			ScenarioSignals* globals = pDoc->_script[SelectedScenario]->globalsignals;
			int i = globals->GetFirstMarkedSignal();
			std::vector<SignalForSortByVal> market_signals;
			std::vector<SignalForSortByVal>::iterator iter, iter2;
			SignalForSortByVal temp;
			while ( i != -1 )
			{
				temp.SignalNumber = i;
				temp.Val = globals->GetSignalPos(i);
				i = globals->GetNextMarkedSignal(i);
				market_signals.push_back(temp);
			}
			std::sort(market_signals.begin(), market_signals.end());
			for (iter = market_signals.begin(); iter != market_signals.end() ; ++iter)
			{
				if (	( p.y < globals->GetSignalPos(iter->SignalNumber) )
					||	( p.y > globals->GetSignalEndPos(iter->SignalNumber) ))
					globals->PushSignalInColumn(iter->SignalNumber, p);
				int disp = globals->GetSignalYSize(iter->SignalNumber);
				if (iter->Val > p.y)
				{
					p.y += disp;
					continue;
				}
				iter2 = iter;
				if (++iter2 != market_signals.end())
				{
					if (iter2->Val > p.y)
						p.y += disp;
				}
				else
					break;
			}
			
			pFrame->GetGraphWnd()->Invalidate();
		}
		else
		{
			num = captured_signal_number;
			if ( pDoc->_script[SelectedScenario]->globalsignals->PushSignalInColumn(num, p) == 0 )
				pFrame->GetGraphWnd()->Invalidate();
		}
	}
	CDC * pDC = GetDC();
	ReadGraphData( pDC );
	ReleaseDC( pDC );
	CaptureType	= UNCAPTURED;
	ReleaseCapture();
}

void GraphButtonView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	((ChildGraphFrame *)GetParentFrame())->GetGraphView()->OnKeyDown(nChar, nRepCnt, nFlags);
}

void GraphButtonView::DrawFullButton( CDC* pDC, int signalnum, int HorizontalSize )
{
	ScenarioSignals* Globalsignals = GetDocument()->_script[SelectedScenario]->globalsignals;
	int SignalPos = Globalsignals->GetSignalPos(signalnum);
	graphdata* Graph = &(GetDocument()->_script[SelectedScenario]->mygraphdata);
	CPen	pen(PS_SOLID,1,RGB(200,200,200));
	CBrush	brsh;
	CString btitle;
	if(Globalsignals->CheckIfSignalMarked(signalnum) == TRUE)
	{
		brsh.CreateSolidBrush(RGB(28,128,128));
		pDC->SetBkColor		(RGB(28,128,128));
	}
	else
	{
		brsh.CreateSolidBrush(RGB(128,128,128));
		pDC->SetBkColor		(RGB(128,128,128));
	}
//---------------------------------------------------------------------------
	
	CFont *NewFont = new CFont();
	CreateFont( NewFont, pDC, signalnum );
	int char_size=10;
	HFONT oldfont	=	(HFONT)pDC->SelectObject( NewFont );
//---------------------------------------------------------------------------
	CPen			*oldpen	=	pDC->SelectObject	(&pen);
	CBrush			*oldbrs	=	pDC->SelectObject	(&brsh);
	pDC->Rectangle(0, SignalPos, HorizontalSize, Globalsignals->GetSignalEndPos( signalnum ));
	pDC->SetTextColor	(RGB(0,0,0));

	int NumberOfSignalsInGroup = Globalsignals->GetSignalsInGroup( signalnum )->GetCount();
	int TextYSize = Globalsignals->GetDistanceBetweenTwoLinesInGroup();
	int WritePosition = ( NumberOfSignalsInGroup > 1 )? SignalPos + (Graph->GetDeltay() - Globalsignals->GetDistanceBetweenTwoLinesInGroup())/2 : SignalPos+1;
	for (int i = 0; i < NumberOfSignalsInGroup; i++ )
	{
		btitle = Globalsignals->GetSignalName( (*Globalsignals->GetSignalsInGroup( signalnum ))[i] );
		if( Globalsignals->CheckIfSignalWasUsed(signalnum)	==	FALSE)
		{
			btitle	+=	" not used";
		}
		int x_start = double(HorizontalSize/2)-(double(pDC->GetTextExtent( btitle ).cx)/2);
		pDC->TextOut(x_start, WritePosition, btitle);
		WritePosition += TextYSize;
	}

	pDC->SelectObject(oldpen);
	pDC->SelectObject(oldbrs);
	pDC->SelectObject(oldfont);
	pen.DeleteObject();
	brsh.DeleteObject();
	NewFont->DeleteObject();
	delete NewFont;
}

int GraphButtonView::GetHorizontalSizeForButtonImage( CImage *ImageForSave )
{
	int HorizontalSize;
	CDC *GraphViewDC;

	ImageForSave->Create( 1, 1, 24 );
	GraphViewDC = CDC::FromHandle( ImageForSave->GetDC() );
	HorizontalSize = GetBiggestSizeOfTexts( GraphViewDC );
	ImageForSave->ReleaseDC();
	ImageForSave->Destroy();
	return HorizontalSize;
}

void GraphButtonView::DrawSignalNamesPanelIntoImage( CImage *ImageForSave )
{
	CDC *GraphViewDC = CDC::FromHandle( ImageForSave->GetDC() );
	DrawSignalNamesPanel( GraphViewDC, ImageForSave->GetWidth() );
	ImageForSave->ReleaseDC();
}

int GraphButtonView::GetBiggestSizeOfTexts( CDC* pDC )
{
	ZWaveeditorDoc* pDoc = GetDocument();
	int Size = 0;
	int TempSize;
	for( int i = 0; i < pDoc->_script[SelectedScenario]->globalsignals->GetNumberOfSignals(); i++ )
	{
		TempSize = GetBiggestSizeOfTextForSignal ( pDC, i );
		if ( TempSize > Size && pDoc->_script[SelectedScenario]->globalsignals->IfShowSignal(i)	== TRUE )
		{
			Size = TempSize;
		}
	}
	return Size;
}

int GraphButtonView::GetBiggestSizeOfTextForSignal ( CDC* pDC, int NumberOfSignal )
{
	ScenarioSignals* Globalsignals = GetDocument()->_script[SelectedScenario]->globalsignals;
	CFont *NewFont = new CFont();
	CreateFont( NewFont, pDC, NumberOfSignal );
	HFONT oldfont	=	(HFONT)pDC->SelectObject( NewFont );
	CArray<int, int> *SignalsInGroup = Globalsignals->GetSignalsInGroup( NumberOfSignal );
	int Size = 0;
	int TempSize;
	for ( int i = 0; i < SignalsInGroup->GetCount(); i++ )
	{
		TempSize = pDC->GetTextExtent( Globalsignals->GetSignalName( (*SignalsInGroup)[i] ) ).cx;
		if ( TempSize > Size )
		{
			Size = TempSize;
		}
	}
	pDC->SelectObject(oldfont);
	NewFont->DeleteObject();
	delete NewFont;
	return Size;
}

void GraphButtonView::CreateFont( CFont * font, CDC* pDC, int NumberOfSignal )
{
	ScenarioSignals* Globalsignals = GetDocument()->_script[SelectedScenario]->globalsignals;
	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));  
	lf.lfHeight = ( Globalsignals->GetSignalsInGroup( NumberOfSignal )->GetCount()> 1 )?
		-Globalsignals->GetDistanceBetweenTwoLinesInGroup() : -(Globalsignals->GetGraphDeltaY() - 5);
	lf.lfWeight = FW_NORMAL;
	lf.lfQuality = PROOF_QUALITY;
	lf.lfWidth = 5;
	lf.lfPitchAndFamily  = DEFAULT_PITCH | FF_ROMAN;
	strcpy(lf.lfFaceName, "Times New Roman");           

	font->CreateFontIndirect(&lf);  // create the font
}

int GraphButtonView::DrawSignalNamesPanel(CDC* pDC, int HorizontalSize)
{
	ScenarioSignals* Globalsignals = GetDocument()->_script[SelectedScenario]->globalsignals;
	for(unsigned int pos= 0;pos<Globalsignals->GetNumberOfSignals();pos++)
	{
		if(Globalsignals->IfShowSignal(pos)== TRUE)
		{
			DrawFullButton( pDC, pos, HorizontalSize);
		}
	}
	return 0;
}

ChildGraphFrame * GraphButtonView::GetParentGraphFrame()
{
	return (ChildGraphFrame *)GetParentFrame();
}

void GraphButtonView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	if ( nSBCode == SB_THUMBTRACK )
	{
		SCROLLINFO ScrollInfo;
		ZeroMemory(&ScrollInfo, sizeof(ScrollInfo));
		ScrollInfo.cbSize = sizeof(ScrollInfo);
		GetScrollInfo( SB_VERT, &ScrollInfo, SIF_ALL );
		CScrollView::OnVScroll(nSBCode, ScrollInfo.nTrackPos, pScrollBar);
	}
	else
	{
		CScrollView::OnVScroll(nSBCode, nPos, pScrollBar);
	}
}

void GraphButtonView::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
{
	CMenu SortContextMenu;
	SortContextMenu.LoadMenu( IDR_WAVES_MENU );
	SortContextMenu.GetSubMenu(0)->TrackPopupMenu( TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, this ); 
}

void GraphButtonView::OnUnGroup()
{
	GetParentGraphFrame()->GetGraphView()->OnUnGroup();
	Invalidate();
	GetParentGraphFrame()->GetGraphView()->Invalidate();
}

void GraphButtonView::OnChooseColorSignal()
{
	GetParentGraphFrame()->GetGraphView()->OnChooseColor();
}

void GraphButtonView::OnRemoveSignal()
{
	GetParentGraphFrame()->GetGraphView()->OnRemoveSignals();
}

void GraphButtonView::OnDeleteSignal()
{
	GetParentGraphFrame()->GetGraphView()->OnDeletePermanently();
}

void GraphButtonView::OnSortSelectedWavesByName()
{
	GetDocument()->_script[SelectedScenario]->globalsignals->OnSortSelectedWavesByName();
	GetParentFrame()->Invalidate();
}

void GraphButtonView::OnSortSelectedWavesByVal()
{
	scenario* my_scenario = GetDocument()->_script[SelectedScenario];
	my_scenario->OnSortSelectedWavesByVal(GetDocument()->GetGroupName());
	GetParentFrame()->Invalidate();
}

void GraphButtonView::OnDisplaceToFirstChangeAndMaxZoomin()
{
	GetParentGraphFrame()->GetGraphView()->DisplaceToFirstChangeAndMaxZoomin();
}

void GraphButtonView::OnDeselectAllWaves()
{
	GetDocument()->_script[SelectedScenario]->globalsignals->RemoveMarksFromSignals();
	Invalidate();
}

void GraphButtonView::OnUpdateSetHighlightingRulesButton( CCmdUI* pCmdUI )
{
	bool IsEnable = ( GetDocument()->_script[SelectedScenario]->globalsignals->GetFirstMarkedSignal() >= 0 );
	pCmdUI->Enable( IsEnable );
}

void GraphButtonView::OnUpdateGroupWavesButton( CCmdUI* pCmdUI )
{
	int SignalNumber = GetDocument()->_script[SelectedScenario]->globalsignals->GetFirstMarkedSignal();
	bool IsEnable = ( GetDocument()->_script[SelectedScenario]->
					  globalsignals->GetNextMarkedSignal( SignalNumber ) >= 0 );
	pCmdUI->Enable( IsEnable );
}

void GraphButtonView::OnUpdateUnGroupButton( CCmdUI* pCmdUI )
{
	bool IsEnable = ( GetDocument()->_script[SelectedScenario]->globalsignals->GetFirstMarkedSignal() >= 0 );
	pCmdUI->Enable( IsEnable );
}

void GraphButtonView::OnUpdateChooseColorSignalButton( CCmdUI* pCmdUI )
{
	bool IsEnable = ( GetDocument()->_script[SelectedScenario]->globalsignals->GetFirstMarkedSignal() >= 0 );
	pCmdUI->Enable( IsEnable );
}
void GraphButtonView::OnUpdateRemoveSignalButton( CCmdUI* pCmdUI )
{
	bool IsEnable = ( GetDocument()->_script[SelectedScenario]->globalsignals->GetFirstMarkedSignal() >= 0 );
	pCmdUI->Enable( IsEnable );
}
void GraphButtonView::OnUpdateDeleteSignalButton( CCmdUI* pCmdUI )
{
	bool IsEnable = ( GetDocument()->_script[SelectedScenario]->globalsignals->GetFirstMarkedSignal() >= 0 );
	pCmdUI->Enable( IsEnable );
}
void GraphButtonView::OnUpdateSortWaveByValButton( CCmdUI* pCmdUI )
{
	int SignalNumber = GetDocument()->_script[SelectedScenario]->globalsignals->GetFirstMarkedSignal();
	bool IsEnable = ( GetDocument()->_script[SelectedScenario]->
					  globalsignals->GetNextMarkedSignal( SignalNumber ) >= 0 );
	pCmdUI->Enable( IsEnable );
}
void GraphButtonView::OnUpdateDisplaceToFirstChangeAndMaxZoominButton( CCmdUI* pCmdUI )
{
	bool IsEnable = ( GetDocument()->_script[SelectedScenario]->globalsignals->GetFirstMarkedSignal() >= 0 );
	pCmdUI->Enable( IsEnable );
}

void GraphButtonView::OnUpdateDeselectAllWavesButton( CCmdUI* pCmdUI )
{
	bool IsEnable = ( GetDocument()->_script[SelectedScenario]->globalsignals->GetFirstMarkedSignal() >= 0 );
	pCmdUI->Enable( IsEnable );
}

void GraphButtonView::OnUpdateSortWaveByNameButton( CCmdUI* pCmdUI )
{
	int SignalNumber = GetDocument()->_script[SelectedScenario]->globalsignals->GetFirstMarkedSignal();
	bool IsEnable = ( GetDocument()->_script[SelectedScenario]->
					  globalsignals->GetNextMarkedSignal( SignalNumber ) >= 0 );
	pCmdUI->Enable( IsEnable );
}


void GraphButtonView::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
	CScrollView::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
	if (!bSysMenu)
    {
		CCmdUI cmdUI;
		cmdUI.m_nIndexMax = pPopupMenu->GetMenuItemCount();
		for (UINT i = 0; i < cmdUI.m_nIndexMax; i++)
		{
			cmdUI.m_nIndex = i;
			cmdUI.m_nID = pPopupMenu->GetMenuItemID(i);
			cmdUI.m_pMenu = pPopupMenu;
			cmdUI.DoUpdate(this, TRUE);   
		}
    }

}

void GraphButtonView::OnGroupWaves()
{
	((ChildGraphFrame*) GetParentFrame())->GetGraphView()->OnGroupWaves();
}

void GraphButtonView::OnSetHighlightingRules()
{
	((ChildGraphFrame*) GetParentFrame())->GetGraphView()->OnSetHighlightingRules();
}
